package com.useeinfo.oa.open.api.maintainOrder;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrder;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2019-01-08
 * @createTime: 16:08:00
 * 工单审核接口
 */
@RestController
@RequestMapping("/api/mobile/audit")
public class AuditOrderAPI {
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;
    @Autowired
    private HttpServletRequest request;

    /**
     * 工单审核
     *
     * @param maintainOrderId 工单ID
     * @param type            结果 0通过 -1 不通过
     * @param remark          留言备注
     * @return
     */
    @RequestMapping("/audit")
    public ResultDto audit(String maintainOrderId, String type, String remark) {
        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        if (null == systemUser) {
            return ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }

        MaintainOrder maintainOrder = maintainOrderBiz.findModel(StringConverters.null2Long(maintainOrderId));
        if (null == maintainOrder) {
            return ResultUtil.error("工单不存在");
        }
        boolean finalAudit = false;
        if (maintainOrder.isAuditing() && maintainOrder.isAuditLast()) {
            finalAudit = true;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("final", finalAudit);

       maintainOrderBiz.audit(maintainOrderId, type, remark, systemUser, request);
        return ResultUtil.success(jsonObject);
    }


    /**
     * 批量审核接口
     *
     * @param maintainOrderIds
     * @param score
     * @param remark
     * @return
     */
    @RequestMapping("/batchAudit")
    public ResultDto batchAudit(String maintainOrderIds, String score, String remark) {
        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        if (null == systemUser) {
            return ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        try {
            return maintainOrderBiz.batchAudit(maintainOrderIds, score, remark, systemUser, request);
        } catch (Exception e) {
            return ResultUtil.error(e.getMessage());
        }

    }

    /**
     * 终极审核打分评价接口
     *
     * @param maintainOrderId
     * @param score
     * @param remark
     * @return
     */
    @RequestMapping("/finalAudit")
    public ResultDto finalAudit(String maintainOrderId, String score, String remark) {
        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        if (null == systemUser) {
            return ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        try {
            return maintainOrderBiz.finalAudit(maintainOrderId, score, remark);
        } catch (Exception e) {
            return ResultUtil.error(e.getMessage());
        }

    }

    /**
     * 判断当前工单是否是完成审核的最终审核
     * @param maintainOrderId
     * @return
     */
    @RequestMapping("/isFinal")
    public ResultDto isFinal(String maintainOrderId) {
        MaintainOrder maintainOrder = maintainOrderBiz.findModel(StringConverters.null2Long(maintainOrderId));
        if (null == maintainOrder) {
            return ResultUtil.error("工单不存在");
        }
        boolean finalAudit = false;
        if (maintainOrder.isAuditing() && maintainOrder.isAuditLast()) {
            finalAudit = true;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("final", finalAudit);
        return ResultUtil.success(jsonObject);
    }
}
